<?php

namespace App\Http\Controllers\Admin;

use App\Models\Fangattr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FangAttrController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Fangattr $fangattr)
    {
        $where = [];
        //起止时间
        $startTime = $request->get('starttime');
        $endTime = $request->get('endtime');
        $name = $request->get('name');
        if (!empty($startTime) && !empty($endTime)) {
            $where = [
                ['created_at', '>=', $startTime],
                ['created_at', '<=', $endTime]
            ];
        }
        if (!empty($name)) {
            $where[] = ['name', 'like', "%{$name}%"];
        }
        $data = $fangattr->getAllList($where);
        $params = [
            "startTime" => $startTime,
            "endTime" => $endTime,
            "name" =>$name,
        ];
        return view("admin.fangattr.index", compact('data','params'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Fangattr $fangattr)
    {
        //
        $data = $fangattr->getAllPidList();
        return view('admin.fangattr.create',compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param Fangattr $fangAttr
     * @return \Illuminate\Http\Response $return
     */
    public function store(Request $request, Fangattr $fangAttr)
    {
        //
        $this->validate($request, [
            'name' => "required",
            'pid' => 'required|numeric'
        ]);
        $data = $request->except('_token', 'icon', 'url');
        $res = imgUpload('icon', "fangAttr");
        if (!$res) {
            return redirect(route('admin.fangattr.create'))->withErrors(['error' => "图片上传失败"]);
        }
        $data['icon'] = $res;
        $data['field_name'] = $data['field_name'] ?: "";
        $fangAttr::create($data);
        return redirect(route('admin.fangattr.index'))->with(['success' => "新增成功"]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function show(Fangattr $fangattr)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function edit(Fangattr $fangattr)
    {
        //
        $data = $fangattr->getAllPidList();
        return view('admin.fangattr.edit', compact('fangattr','data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Fangattr $fangattr)
    {
        //
        $this->validate($request, [
            'name' => "required",
            'pid' => 'required|numeric'
        ]);
        $data = $request->except('_token', 'icon', 'url','_method');
        if ($_FILES['icon']['tmp_name']){
            $res = imgUpload('icon', "fangAttr");
            if (!$res) {
                return redirect(route('admin.fangattr.index'))->withErrors(['error' => "图片上传失败"]);
            }
            $data['icon'] = $res;
        }
        $data['field_name'] = $data['field_name'] ?: "";
        $fangattr->update($data);
        return redirect(route('admin.fangattr.index'))->with(['success' => "修改成功"]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function destroy(Fangattr $fangattr)
    {
        $res = $fangattr->delete();
        if ($res) {
            success([], '删除成功', 200);
        } else {
            fail(400, '删除失败');
        }
    }
}
